package eclipse.client.forms;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import eclipse.client.*;
import eclipse.lib.sockets.*;
public class ServerList
{
	private static JFrame 		window 		= new JFrame("Servers");
	private static JComboBox 	cmbServer 	= new JComboBox();	
	private static JButton 		btnConnect	= new JButton("Connect");
	private static JPanel 		pnlOptions	= new JPanel();	
	public static void makeGUI()
	{		
		for(int index = 0; index < Database.MAXSERVERS; index++)
			cmbServer.addItem(Database.SERVERNAME.get(index) + 
			" (" + Database.SERVERIP.get(index) + 
			":" + Database.SERVERPORT.get(index)+ ")"		
			);				
		
		btnConnect.addActionListener(new ActionEventListener());			
		window.setSize(240, 120);
		window.setLocation(300, 200);
		window.setLayout(new GridLayout(2,1));
		window.add(cmbServer);
		window.add(pnlOptions);
		window.addWindowListener
		(
			new WindowAdapter()
			{
				public void windowClosing(WindowEvent ev)
				{					
					window.removeAll();
					System.gc();
					System.exit(0);
				}
			}			
		);
		pnlOptions.setLayout(new GridLayout(1, 2));
		pnlOptions.add(Database.threadCache.process_ConCThread.getStatusLabel());
		pnlOptions.add(btnConnect);
		window.setAlwaysOnTop(true);		
	}
	public static void showForm()
	{
		window.setVisible(true);
	}
	public static void hideForm()
	{
		window.setVisible(false);
	}
	private static class ActionEventListener implements ActionListener
	{
		public void actionPerformed(ActionEvent event) 
		{
			if(event.getSource().equals(btnConnect));
			{
				if(Database.Socket.isConnected())
				{
					ClientDataHandle.requestMaxData();
					hideForm();
					Database.MainMenu.setVisible(true);	
					Database.threadCache.process_ConCThread.setRunStatus(false);					
				}
			}
		}		
	}	
}
